<?php
namespace Home\Controller;
use Home\Model\ApinfoModel;
use Home\Model\SqliteApinfo;
use Home\Model\SqliteStainfo;
use Home\Model\SqliteApinfo2;
use Home\Model\SqliteStainfo2;
use Home\Model\PassbyPushModel;
use Think\Controller;
class ScanwifiController extends BaseController {

    public function DatabaseClearAP(){
        if(IS_POST){
            $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_scan]['cardip'] . C('LUCI_WIFISCAN_URL') . 'DatabaseClear/APInfo', null);
            $js=(json_decode($retdata, true));
            D('Apinfo')->where('1')->delete();

            $this->ajaxReturn(array('status'=>$js[0]), 'JSON');
        }
    }

    public function DatabaseClearSTA(){
        if(IS_POST){
            $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_scan]['cardip'] . C('LUCI_WIFISCAN_URL') . 'DatabaseClear/STAInfo', null);
            $js=(json_decode($retdata, true));
            D('Stainfo')->where('1')->delete();

            $this->ajaxReturn(array('status'=>$js[0]), 'JSON');
        }
    }

    public function getscanstatus(){
        //获取扫描子板的获取ap列表接口
        if(IS_POST){
            $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_scan]['cardip'] . C('LUCI_WIFISCAN_URL') . 'ConfigScan/status', null);
            $js=(json_decode($retdata, true));
            $this->ajaxReturn(array('scanstatus'=>$js[0]), 'JSON');
        }
    }


    public function  doscan(){
        if(IS_POST){
            $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_scan]['cardip'] . C('LUCI_WIFISCAN_URL') . 'ConfigScan/' . I('post.action'), null);
            $js=(json_decode($retdata, true));

            //$this->display('scanwifi:scanwifi');
            $this->ajaxReturn(array('status'=>$js[0]), 'JSON');
        }
    }
    //获取重点目标告警
    public function getappearmac(){
        if(IS_POST){
            $mac=D('macalert')->where(array('valid'=>1))->select();
            if(!$mac){
                $this->ajaxReturn(array('status'=>false, 'data'=>''), 'JSON');
            }
            else{
                $sqliteinfo=new SqliteStainfo();
                $dates10 = date("YmdHis" ,time()-60 ).'';
                $hasdata=0;
                $index=0;
                for($iloop=0;$iloop<count($mac);$iloop++){
                    $data = $sqliteinfo->where('s_mac="'.$mac[$iloop]['mac'] .'" and time >="'.$dates10.'"')->limit(1)->select();
                    //"s_mac='%s' and time >='%s'",array($mac[$iloop]['mac'], $dates10)
                    if($data){
                        $apdata[$index] = $data[0];
                        $apdata[$index]['alertname']=$mac[$iloop]['alertname'];
                        $apdata[$index]['timedesc']=substr($apdata[$index]['time'],0,4).'/'.substr($apdata[$index]['time'],4,2).'/'
                            .substr($apdata[$index]['time'],6,2).' '
                            .substr($apdata[$index]['time'],8,2).':'
                            .substr($apdata[$index]['time'],10,2).':'
                            .substr($apdata[$index]['time'],12,2).'';
                        $hasdata=1;
                        $index++;
                    }
                }

                $dates=date("YmdHis" ,time() );

                if($hasdata==1){
                    $this->ajaxReturn(array('status'=>true, 'data'=>$apdata ), 'JSON');
                }
                else{
                    $this->ajaxReturn(array('status'=>false, 'data'=>''), 'JSON');
                }
            }
        }
    }

    public function getdata(){
        //if(IS_POST)
        {
            $sqliteapinfo=new SqliteApinfo();
            $apdata= $sqliteapinfo->order(array( 'time'=>'desc'))->select();



            $apinfodatable= D('Apinfo');
            $apinfodatable->where(1)->delete();
            $count=count($apdata);
            //$map="";
            if($apdata){
                for($iloop=0; $iloop < $count; $iloop++){
                    //$apdata[$iloop]['essid'] = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($apdata[$iloop]['essid']));
                    $map[$iloop]= array(
                        'id'=>$iloop+1,
                        'ssid'=>base64_encode( $apdata[$iloop]['essid']),
                        'mac'=>$apdata[$iloop]['s_mac'],
                        'signalpath'=>$apdata[$iloop]['channel'],
                        'devmodel'=>$apdata[$iloop]['device_name'],
                        'ver_name'=>$apdata[$iloop]['ver_name'],
                        'time'=>$apdata[$iloop]['time'],
                        'encryption'=>str_replace("/","-",$apdata[$iloop]['en_mode'] ),
                        'signalstrength'=>$apdata[$iloop]['signal'],
                        'distance'=>'['.round(pow(10,abs(intval($apdata[$iloop]['signal']))
                                    /(10*4))*10)/100 . '米]',
                        'timeparse'=>substr($apdata[$iloop]['time'],0,4).'/'.substr($apdata[$iloop]['time'],4,2).'/'
                            .substr($apdata[$iloop]['time'],6,2).' '
                            .substr($apdata[$iloop]['time'],8,2).':'
                            .substr($apdata[$iloop]['time'],10,2).':'
                            .substr($apdata[$iloop]['time'],12,2).'',
                        'gps'=>$apdata[$iloop]['position']
                    );
                }
            }
            if(C('TWOSCANCARD')=='true'){
                $sqliteapinfo2=new SqliteApinfo2();
                $apdata= $sqliteapinfo2->order(array( 'time'=>'desc'))->select();
                $count2=count($apdata);
                if($apdata){
                    for($iloop=0; $iloop < $count2; $iloop++){
                        $map[$count+$iloop]= array(
                            'id'=>$iloop+1,
                            'ssid'=>base64_encode( $apdata[$iloop]['essid']),
                            'mac'=>$apdata[$iloop]['s_mac'],
                            'signalpath'=>$apdata[$iloop]['channel'],
                            'devmodel'=>$apdata[$iloop]['device_name'],
                            'ver_name'=>$apdata[$iloop]['ver_name'],
                            'time'=>$apdata[$iloop]['time'],
                            'encryption'=>str_replace("/","-",$apdata[$iloop]['en_mode'] ),
                            'signalstrength'=>$apdata[$iloop]['signal'],
                            'distance'=>'['.round(pow(10,abs(intval($apdata[$iloop]['signal']))
                                        /(10*4))*10)/100 . '米]',
                            'timeparse'=>substr($apdata[$iloop]['time'],0,4).'/'.substr($apdata[$iloop]['time'],4,2).'/'
                                .substr($apdata[$iloop]['time'],6,2).' '
                                .substr($apdata[$iloop]['time'],8,2).':'
                                .substr($apdata[$iloop]['time'],10,2).':'
                                .substr($apdata[$iloop]['time'],12,2).'',
                            'gps'=>$apdata[$iloop]['position']
                        );
                    }
                }
                //对数据进行排序
                array_multisort(array_column($map,'time'),SORT_DESC,$map);
                for($loop=0;$loop<count($map);$loop++){
                    $map[$loop]['id']=$loop+1;
                }
            }
            if($map==null){
                $map=[];
            }
            $this->ajaxReturn(array('data'=>$map ), 'JSON');
        }
    }

    public function scanwifi(){

        $this->assign('title','热点扫描');
        $this->assign('currentname','scanwifi');

        //设置脚本最大执行时间
        /*$sqliteapinfo=new SqliteApinfo();
        $apdata= $sqliteapinfo->select();

        $apinfodatable= D('Apinfo');
        $apinfodatable->where(1)->delete();
        $count=count($apdata);

        if($apdata){
            for($iloop=0; $iloop < $count; $iloop++){
                $map[$iloop]= array(
                    'ssid'=>$apdata[$iloop]['essid'],
                    'mac'=>$apdata[$iloop]['s_mac'],
                    'signalpath'=>$apdata[$iloop]['channel'],
                    'devmodel'=>$apdata[$iloop]['device_name'],
                    'ver_name'=>$apdata[$iloop]['ver_name'],
                    'time'=>$apdata[$iloop]['time'],
                    'encryption'=>str_replace("/","-",$apdata[$iloop]['en_mode'] ),
                    'signalstrength'=>$apdata[$iloop]['signal']
                );
            }
        }
        if($count){
            $apinfodatable->addAll($map);
        }

        $apinfodata = $apinfodatable->order(array( 'time'=>'desc'))->limit(1,$count)->select();
        if($apinfodata==true){
            $this->assign("scanwifidata",$apinfodata);
        }
        //获取是否有设备正在破解、攻击中
        $iscrack = $apinfodatable->where(array('iscrack'=>1))->count();
        $this->assign("iscrack",$iscrack);
        $isattack = $apinfodatable->where(array('isattack'=>1))->count();
        $this->assign("isattack",$isattack);
        $iswz = $apinfodatable->where(array('iswz'=>1))->count();
        $this->assign("iswz",$iswz);
        */
        $this->display();

        return;

        //获取热点和终端列表
        $url=$_SESSION['cardinfo'][self::$cardtype_scan]['cardip'] . C('LUCI_WIFISCAN_URL') . 'APDevInfo/nil%20nil%20nil%20nil%20nil%20';

        $retwifidata = http_post_json($url, null);
        $jswifidata=(json_decode($retwifidata, true));
        if($jswifidata){
            for($iloop=0; $iloop < count($jswifidata[0]); $iloop++){
                $map= array(
                    'ssid'=>$jswifidata[0][$iloop]['ssid'],
                    'mac'=>$jswifidata[0][$iloop]['mac'],
                    'signalpath'=>$jswifidata[0][$iloop]['signalPath'],
                    'devmodel'=>$jswifidata[0][$iloop]['devmodel'],
                    'time'=>$jswifidata[0][$iloop]['time'],
                    'encryption'=>str_replace("/","-",$jswifidata[0][$iloop]['encryption'] ),
                    'signalstrength'=>$jswifidata[0][$iloop]['signalStrength']
                );
                //先查询是否存在相同ssid名称，相同mac
                if(D('Apinfo')->getSimCount($jswifidata[0][$iloop]['ssid'],  $jswifidata[0][$iloop]['mac'])<=0){
                    D('Apinfo')->add($map);
                }
                else{
                    D('Apinfo')->where(array('mac'=>$map['mac']))->save($map);
                }
            }
        }

        $apinfodata = D('Apinfo')->order(array( 'time'=>'desc'))->select();
        if($apinfodata==true){
            $this->assign("scanwifidata",$apinfodata);
        }
        //获取是否有设备正在破解、攻击中
        $iscrack = D('Apinfo')->where(array('iscrack'=>1))->count();
        $this->assign("iscrack",$iscrack);
        $isattack = D('Apinfo')->where(array('isattack'=>1))->count();
        $this->assign("isattack",$isattack);
        $iswz = D('Apinfo')->where(array('iswz'=>1))->count();
        $this->assign("iswz",$iswz);


        //获取热点和终端列表
        /*
        $retteerminaldata = http_post_json($_SESSION['cardinfo'][self::$cardtype_scan]['cardip'] . C('LUCI_WIFISCAN_URL') . 'TerminalDevInfo/nil%20nil%20nil%20nil%20nil%20', null);
        $jsterminaldata=(json_decode($retteerminaldata, true));

        $this->assign("scanterminaldata",$jsterminaldata[0]);
        */

        $this->display();
    }

    public function getstatus(){
        if(IS_POST){
            $optwifiinfo=D('optwifiinfo');
            if(I('post.mac')){
                $crackdata=$optwifiinfo->where(array('crack'=>1,'mac'=>I('post.mac')))->select();
                $wzdata=$optwifiinfo->where(array('wz'=>1,'mac'=>I('post.mac')))->select();
                $attackdata=$optwifiinfo->where(array('attack'=>1,'mac'=>I('post.mac')))->select();
                $yazhidata=$optwifiinfo->where(array('yazhi'=>1,'mac'=>I('post.mac')))->select();
                $map=array('crack'=>$crackdata?$crackdata[0]:'',
                    'wz'=>$wzdata?$wzdata[0]:'',
                    'attack'=>$attackdata?$attackdata[0]:'',
                    'yazhi'=>$yazhidata?$yazhidata[0]:'');
                $this->ajaxReturn(array('status'=>true, 'data'=>$map), 'JSON');
            }else{
                $crackdata=$optwifiinfo->where(array('crack'=>1))->select();
                $wzdata=$optwifiinfo->where(array('wz'=>1))->select();
                $attackdata=$optwifiinfo->where(array('attack'=>1))->select();
                $yazhidata=$optwifiinfo->where(array('yazhi'=>1))->select();
                $map=array('crack'=>$crackdata?$crackdata[0]:'',
                    'wz'=>$wzdata?$wzdata[0]:'',
                    'attack'=>$attackdata?$attackdata[0]:'',
                    'yazhi'=>$yazhidata?$yazhidata[0]:'');
                $this->ajaxReturn(array('status'=>true, 'data'=>$map), 'JSON');
            }
        }
    }
    public function getgpsbymac(){
        if(IS_POST){
            $sqliteapinfo=new SqliteApinfo();
            $stadata= $sqliteapinfo->where(array('s_mac="'.I('post.mac').'"') )->order(array( 'time'=>'desc'))->limit(1)->select();

            $this->ajaxReturn(array('data'=>$stadata), 'JSON');
        }
    }

    public function yazhi(){
        //使用攻击组件
       // if(IS_POST)
        {
            if(I('post.action')=='stop'){
                self::setstatus(I('post.action'),'yazhi', I('post.ssid'),  I('post.xd'), I('post.encrypt'), '', I('post.srcmac'), 0, '', 0, 0, 2);
            }
            else{
                self::setstatus(I('post.action'),'yazhi', I('post.ssid'),  I('post.xd'), I('post.encrypt'), '', I('post.srcmac'), 0, '', 0, 0, 1);
            }
            if(I('post.signalPath')=='-1'){
                $sqliteapinfo=new SqliteApinfo();
                $apdata= $sqliteapinfo->where(array('s_mac="'.I('post.apmac').'"') )->order(array('time'=>'desc'))->select();
                if($apdata){
                    if($apdata[0]['s_mac']=='ff:ff:ff:ff:ff'){
                        $this->ajaxReturn(array('data'=>'', 'status'=>false), 'JSON');
                        return;
                    }

                    $action=I('post.action');
                    $signalPath=$apdata[0]['channel'];
                    $apmac=I('post.apmac');
                    $srcmac=I('post.srcmac');
                    $url=$_SESSION['cardinfo'][self::$cardtype_attack]['cardip'] . C('LUCI_WIFISCAN_URL') . 'STAAttackAct/'.$action.'/'.$signalPath.'/'.$apmac.'/'.$srcmac;
                    $retdata = http_post_json($url, null);
                    $jsdata = json_decode($retdata,TRUE);
                    $this->ajaxReturn(array('data'=>$jsdata, 'status'=>true, 'url'=>$url), 'JSON');
                }
                else{
                    $this->ajaxReturn(array('data'=>'', 'status'=>false), 'JSON');
                }
            }
            else{
                $action=I('post.action');
                $signalPath=I('post.signalPath');
                $apmac=I('post.apmac');
                $srcmac=I('post.srcmac');
                $url=$_SESSION['cardinfo'][self::$cardtype_attack]['cardip'] . C('LUCI_WIFISCAN_URL') . 'STAAttackAct/'.$action.'/'.$signalPath.'/'.$apmac.'/'.$srcmac;
                $retdata = http_post_json($url, null);
                $jsdata = json_decode($retdata,TRUE);
                $this->ajaxReturn(array('data'=>$jsdata, 'status'=>true, 'url'=>$url), 'JSON');
            }


        }
    }

    public function setstatus($action,$type, $ssid, $channel, $encrytype, $encrykey, $mac, $crack, $crackpwd, $wz, $attack, $yazhi){
        $optwifiinfo=D('optwifiinfo');
        $map=array('ssid'=>$ssid,
            'channel'=>$channel,
            'encrytype'=>$encrytype,
            'encrykey'=>$encrykey,
            'mac'=>$mac);
        if($type=='crack'){
            $map['crack']=$crack;
        }
        if($type=='wz'){
            $map['wz']=$wz;
        }
        if($type=='attack'){
            $map['attack']=$attack;
        }
        if($type=='yazhi'){
            $map['yazhi']=$yazhi;
        }
        if($action=='start'){
            $map['starttime']=date("Y-m-d H:i:s" ,time());
            if($type=='crack'){
                $optwifiinfo->where(array('crack'=>1))->save(array('crack'=>2,'stoptime'=>date("Y-m-d H:i:s" ,time())));
            }
            if($type=='wz'){
                $optwifiinfo->where(array('wz'=>1))->save(array('wz'=>2,'stoptime'=>date("Y-m-d H:i:s" ,time())));
            }
            if($type=='attack'){
                $optwifiinfo->where(array('attack'=>1))->save(array('attack'=>2,'stoptime'=>date("Y-m-d H:i:s" ,time())));
            }
            if($type=='yazhi'){
                $optwifiinfo->where(array('yazhi'=>1))->save(array('yazhi'=>2,'stoptime'=>date("Y-m-d H:i:s" ,time())));
            }

            $optwifiinfo->add($map);
        }else{
            $map['stoptime']=date("Y-m-d H:i:s" ,time());
            $map['crackpwd']=$crackpwd;
            if($type=='crack'){
                $optwifiinfo->where(array('mac'=>$mac, 'crack'=>1))->save($map);
            }
            if($type=='wz'){
                $optwifiinfo->where(array( 'wz'=>1))->save($map);
            }
            if($type=='attack'){
                $optwifiinfo->where(array('mac'=>$mac, 'attack'=>1))->save($map);
            }
            if($type=='yazhi'){
                $optwifiinfo->where(array('mac'=>$mac, 'yazhi'=>1))->save($map);
            }
        }

    }

    public function getdevlist(){
        if(IS_POST){
            //http://192.168.11.1/cgi-bin/luci/admin/wifiscan/TerminalDevInfo/nil%2088:25:93:B9:94:00%20nil%20nil%20
            $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_scan]['cardip'] . C('LUCI_WIFISCAN_URL')
                . 'TerminalDevInfo/nil%20' . I('post.apmac') ."%20nil%20nil%20", null);
            $js=(json_decode($retdata, true));

            //$this->display('scanwifi:scanwifi');
            //$this->ajaxReturn(array('status'=>$js[0]), 'JSON');

            //$data=D('stainfo')->where(array('dmac'=>I('post.apmac')))->select();
            if($js){
                $this->ajaxReturn(array('status'=>true, 'data'=>$js[0]), 'JSON');
            }
            else{
                $this->ajaxReturn(array('status'=>false, 'data'=>null), 'JSON');
            }
        }
    }


    public function  scan(){
        if(IS_POST){
            $setcardstatus= [];
            $ip[0]=I('post.ip0');
            $ip[1]=I('post.ip1');
            $ip[2]=I('post.ip2');
            $ip[3]=I('post.ip3');
            for($i=0; $i < 4; $i++){
                $retdata = http_post_json( $ip[$i] . C('LUCI_STATUS_URL') . 'getStatInfo', null);
                $js=(json_decode($retdata, true));
                $setcardstatus[$i]=$js[0][0];
            }

            //$this->assign('setcardstatus', $setcardstatus);

            $this->ajaxReturn($setcardstatus, 'JSON');
        }
    }




    /////////////////////////////////////////////////////
    public function  stopPackageAct(){
        if(IS_POST){
            $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageAct/stop', null);
            $js=(json_decode($retdata, true));

            $this->ajaxReturn(array('status'=>$js[0]), 'JSON');
        }
    }
    public function  startPackageAct(){
        if(IS_POST){
            $retdatas = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageDatabaseClearSTA', null);

            $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageAct/start', null);
            $js=(json_decode($retdata, true));

            $this->ajaxReturn(array('status'=>$js[0]), 'JSON');
        }
    }
    public function  PackageList(){
        if(IS_POST){
            $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageList', null);
            $js=(json_decode($retdata, true));

            $this->ajaxReturn(array('status'=>$js[0]), 'JSON');
        }
    }
    public function  PackageClear(){
        if(IS_POST){
            $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageClear', null);
            $js=(json_decode($retdata, true));

            $this->ajaxReturn(array('status'=>$js[0]), 'JSON');
        }
    }



    public function crackWiFi(){
        if(IS_POST){
            if(I('post.action')=='stop'){
                self::setstatus(I('post.action') ,'crack', I('post.ssid'),  I('post.xd'), I('post.encrypt'), '', I('post.apmac'), 2, '', 0, 0, 0);

                $status=I('post.action')=='start'?'1':'0';
                D('apinfo')->where(array('mac'=>I('post.apmac')))->save(array('iscrack'=>$status));
                $this->ajaxReturn(array('status'=>true), 'JSON');
            }
            else if(I('post.xd')){
                self::setstatus(I('post.action'),'crack', I('post.ssid'),  I('post.xd'), I('post.encrypt'), '', I('post.apmac'), 1, '', 0, 0, 0);

                $status=I('post.action')=='start'?'1':'0';
                D('apinfo')->where(array('mac'=>I('post.apmac')))->save(array('iscrack'=>$status));
                //清除历史抓包 开始抓包---》10s---》3、停止抓包----》
                //（校验：暂时没有接口）---》4、获取到抓包文件（samba的挂载磁盘方式，先配置好固定的子板）
                // -------》5、破解调用---》6、等待返回结果数据（成功或失败）----》7、展示破解结果：成功/失败！

                //0.停止抓包
                $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageAct/stop', null);

                //1.清除历史抓包
                $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageClear', null);
                //2.开始抓包
                $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageAct/start/'.I('post.xd').'/'. I('post.apmac'), null);

                //10s
                sleep(10);
                //3.停止抓包
                $retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageAct/stop', null);

                //4.获取到抓包文件
                ////$retdata = http_post_json($_SESSION['cardinfo'][self::$cardtype_package]['cardip'] . C('LUCI_WIFISCAN_URL') . 'PackageAct/PackageList', null);
                //var_dump($retdata);
                //test.hccapx
                //crackfilepath
                //5.调用破解
                $filename='/crackpath/cap/test.hccapx';
                if (!file_exists($filename) || filesize($filename)<=0) {
                    self::setstatus('stop' ,'crack', I('post.ssid'),  I('post.xd'), I('post.encrypt'), '', I('post.apmac'), 2, '', 0, 0, 0);
                    $this->ajaxReturn(array('status'=>false, 'reason'=>'破解期间没有握手报文。'), 'JSON');
                    return;
                }

                $startcrackjson= json_encode(array('taskid'=>I('post.id'),
                    'crackfilepath'=>$filename));
                $retdata = http_post_json('http://127.0.0.1:5000/crackwifi/start', $startcrackjson);
                $retdata=(json_decode($retdata, true));
                //6.等待返回结果数据
                //7.展示破解结果：成功/失败
                //将破解的秘钥保存到apinfo表


                $this->ajaxReturn($retdata, 'JSON');

            }
            else{
                $this->ajaxReturn(array('status'=>false), 'JSON');
            }
        }
    }

    public function getcrackWiFiStatus(){
        if(IS_POST){
            $startcrackjson=json_encode(array('taskid'=>I('post.id')));
            $retdata = http_post_json('http://127.0.0.1:5000/crackwifi/status', $startcrackjson);
            $retdata=(json_decode($retdata, true));
            if($retdata['status']=='success'){
                $crackpwd= end(explode(':', $retdata['secret'])) ;

                self::setstatus('stop' ,'crack', I('post.ssid'),  I('post.xd'), I('post.encrypt'), '', I('post.apmac'), 2, $crackpwd, 0, 0, 0);

                $retdata['secret']=$crackpwd;
            }
            else if($retdata['status']=='run'){

            }else{
                $status=0;
                self::setstatus('stop' ,'crack', I('post.ssid'),  I('post.xd'), I('post.encrypt'), '', I('post.apmac'), 2, '', 0, 0, 0);

            }

            $this->ajaxReturn($retdata, 'JSON');
        }
    }
    public  function stopcrackWiFi(){

    }

    public function getStrength(){
        //$this->ajaxReturn(array('signalstrength'=>'-85'), 'JSON');
        if(IS_POST){
            $mac= mb_convert_case (I('post.mac'),MB_CASE_LOWER, "UTF-8");

            $url=$_SESSION['cardinfo'][self::$cardtype_scan]['cardip'] . C('LUCI_WIFISCAN_URL') .
                'GetDis/'.I('post.action').'/' .$mac;

            $retdata = http_post_json($url, null);
            $js=(json_decode($retdata, true));
            $signalstrength = $js[0];


            $signalstrength = str_replace(array("\r\n", "\r", "\n"), "", $signalstrength);
            $signalstrength = str_replace(array(" "), "", $signalstrength);
            $this->ajaxReturn(array('signalstrength'=>$signalstrength), 'JSON');
        }
    }

    public function attackWiFi(){
        if(IS_POST){
            if(I('post.action')=='start'){
                self::setstatus(I('post.action'),'attack', I('post.ssid'),  I('post.xd'), I('post.encrypt'), '', I('post.apmac'), 0, '', 0, 1, 0);
            }
            else{
                self::setstatus(I('post.action'),'attack', I('post.ssid'),  I('post.xd'), I('post.encrypt'), '', I('post.apmac'), 0, '', 0, 2, 0);
            }


            $url=$_SESSION['cardinfo'][self::$cardtype_attack]['cardip'] . C('LUCI_WIFISCAN_URL') .
                'APAttackAct/'.I('post.action').'/'.I('post.xd').'/'. I('post.apmac');

            $retdata = http_post_json($url, null);
            $js=(json_decode($retdata, true));

            //$status=I('post.action')=='start'?'1':'0';
            //D('apinfo')->where(array('mac'=>I('post.apmac')))->save(array('isattack'=>$status));


            $this->ajaxReturn(array('status'=>$js[0]), 'JSON');
        }
    }

    public function WZWifi(){
        $status=I('post.action')=='start'?'1':'0';
        if(IS_POST){
            if(I('post.action')=='start'){
                $url=$_SESSION['cardinfo'][self::$cardtype_wz]['cardip'] . C('LUCI_WIFISCAN_URL') . 'WifiBypass/stop'
                    .'////';
                $ret = http_post_json($url, null);
                $model=new PassbyPushModel();
                $list=$model->closePassbyPush('self');
                sleep(1);
            }


            $url=$_SESSION['cardinfo'][self::$cardtype_wz]['cardip'] .
                C('LUCI_WIFISCAN_URL') . 'WZWifi/'
                . I('post.action').'/'.I('post.xd').'/'. I('post.ssid').'/'. I('post.encrypt').'/'. I('post.key');



            $retdata = http_post_json($url, null);
            $js=(json_decode($retdata, true));

            if(I('post.action')=='start'){
                self::setstatus(I('post.action'),'wz', I('post.ssid'),  I('post.xd'), I('post.encrypt'), I('post.key'),  I('post.apmac'), 0, '', 1, 0, 0);

            }else{
                self::setstatus(I('post.action'),'wz', I('post.ssid'),  I('post.xd'), I('post.encrypt'), I('post.key'),  I('post.apmac'), 0, '', 2, 0, 0);
            }

            $this->ajaxReturn(array('status'=>$js), 'JSON');
        }
    }

    public function clearwzwifi(){
        if(IS_POST){
            $optwifiinfo=D('optwifiinfo');
            $optwifiinfo->where(1)->save(array('wz'=>2));
            $this->ajaxReturn(array('status'=>true), 'JSON');

        }
    }
}